from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.validators import UnicodeUsernameValidator
from django.utils.translation import gettext_lazy as _
from extends.model.base import TimeStampedModel


# 用户
class ChatUser(AbstractUser):
    username_validator = UnicodeUsernameValidator()
    username = models.CharField(
        _("username"),
        max_length=150,
        unique=True,
        help_text=_(
            "Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only."
        ),
        validators=[username_validator],
        error_messages={
            "unique": _("A user with that username already exists."),
        },
    )
    phone = models.CharField(_("phone"),unique=True, max_length=20)

    class Meta:
        db_table = "chat_user"
    

# 群
class ChatGroup(TimeStampedModel):
    id = models.AutoField(primary_key=True)
    name = models.CharField(_("group name"),max_length=200)
    leader = models.IntegerField(_("leader"), default=0)
    number_of_people = models.IntegerField(_("number of people"), default=0)

    class Meta:
        db_table = "chat_group"


# 用户加入的群组
class UserToGroup(TimeStampedModel):
    id = models.AutoField(primary_key=True)
    user_id = models.IntegerField(_("user id"), default=0)
    group_id = models.IntegerField(_("group id"), default=0)

    class Meta:
        db_table = "user_to_group"


# 消息
class ChatMessage(TimeStampedModel):
    id = models.AutoField(primary_key=True)
    user_id = models.IntegerField(_("user id"), default=0)
    group_id = models.IntegerField(_("group id"), default=0)
    sender = models.CharField(_("发件人"),max_length=200)
    content = models.CharField(_("内容"),max_length=2000)
    withdraw = models.BooleanField(_("撤回"),default=False)

    class Meta:
        db_table = "chat_message"
    